package com.hotel.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotel.common.Result;
import com.hotel.dto.RoomTypeDTO;
import com.hotel.entity.RoomType;
import com.hotel.service.RoomTypeService;
import com.hotel.vo.RoomTypeVO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 房型管理控制器
 * 
 * @author Hotel Management Team
 */
@RestController
@RequestMapping("/roomtype")
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
public class RoomTypeController {

    private final RoomTypeService roomTypeService;

    /**
     * 分页查询房型列表
     */
    @GetMapping("/list")
    public Result<Page<RoomTypeVO>> getRoomTypePage(
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "20") int size,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String status) {
        Page<RoomTypeVO> page = roomTypeService.getRoomTypePage(current, size, keyword, status);
        return Result.success(page);
    }

    /**
     * 获取房型详情
     */
    @GetMapping("/{id}")
    public Result<RoomTypeVO> getRoomTypeById(@PathVariable Long id) {
        RoomTypeVO roomType = roomTypeService.getRoomTypeById(id);
        return Result.success(roomType);
    }

    /**
     * 根据名称获取房型
     */
    @GetMapping("/name/{name}")
    public Result<RoomType> getRoomTypeByName(@PathVariable String name) {

        RoomType roomType = roomTypeService.getRoomTypeByName(name);
        return Result.success(roomType);
    }

    /**
     * 创建房型
     */
    @PostMapping
    public Result<Void> createRoomType(@Valid @RequestBody RoomTypeDTO roomType) {
        boolean success = roomTypeService.createRoomType(roomType);
        if (success) {
            return Result.success("房型创建成功");
        } else {
            return Result.error("房型创建失败");
        }
    }

    /**
     * 更新房型
     */
    @PutMapping("/{id}")
    public Result<Void> updateRoomType(@PathVariable Long id, @Valid @RequestBody RoomTypeDTO roomType) {
        //roomType.setId(id);
        boolean success = roomTypeService.updateRoomType(id,roomType);
        if (success) {
            return Result.success("房型更新成功");
        } else {
            return Result.error("房型更新失败");
        }
    }

    /**
     * 删除房型
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteRoomType(@PathVariable Long id) {
        boolean success = roomTypeService.deleteRoomType(id);
        if (success) {
            return Result.success("房型删除成功");
        } else {
            return Result.error("房型删除失败，可能存在关联数据");
        }
    }

    /**
     * 获取启用的房型列表
     */
    @GetMapping("/enabled")
    public Result<List<RoomTypeVO>> getEnabledRoomTypes() {
        List<RoomTypeVO> roomTypes = roomTypeService.getEnabledRoomTypes();
        return Result.success(roomTypes);
    }

    /**
     * 更新房型状态
     */
    @PutMapping("/{id}/status")
    public Result<Void> updateRoomTypeStatus(@PathVariable Long id, @RequestParam String status) {
        boolean success = roomTypeService.updateRoomTypeStatus(id, status);
        if (success) {
            return Result.success("房型状态更新成功");
        } else {
            return Result.error("房型状态更新失败");
        }
    }

    /**
     * 获取房型统计信息
     */
    @GetMapping("/stats")
    public Result<Map<String, Object>> getRoomTypeStats() {
        Map<String, Object> stats = roomTypeService.getRoomTypeStats();
        return Result.success(stats);
    }

    /**
     * 获取房型房间数量统计
     */
    @GetMapping("/room-count")
    public Result<List<Map<String, Object>>> getRoomTypeRoomCount() {
        List<Map<String, Object>> roomCount = roomTypeService.getRoomTypeRoomCount();
        return Result.success(roomCount);
    }

    /**
     * 检查房型是否在使用中
     */
    @GetMapping("/{id}/in-use")
    public Result<Boolean> isRoomTypeInUse(@PathVariable Long id) {
        boolean inUse = roomTypeService.isRoomTypeInUse(id);
        return Result.success(inUse);
    }

    /**
     * 获取房型价格历史
     */
    @GetMapping("/{id}/price-history")
    public Result<List<Map<String, Object>>> getRoomTypePriceHistory(@PathVariable Long id) {
        List<Map<String, Object>> priceHistory = roomTypeService.getRoomTypePriceHistory(id);
        return Result.success(priceHistory);
    }
}